//
// Created on 2024/9/6.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef OHOS_DEMO_RTC_LOG_H
#define OHOS_DEMO_RTC_LOG_H

#include <numeric>
#include <sstream>
#include <iostream>
#include <thread>
#include <chrono>
#include <iomanip>

#if defined(OS_OHOS)
#include "hilog/log.h"
#undef LOG_DOMAIN
#undef LOG_TAG
#define LOG_DOMAIN 0x3200  // 全局domain宏，标识业务领域
#define LOG_TAG "RTC_LOG"   // 全局tag宏，标识模块日志tag
#endif // defined(OS_OHOS)

std::string GetTimestamp();

class LogRecord {
 public:

  LogRecord(const char *file, size_t line, const char *func) {
    ss_ << "RTC_LOG " << GetTimestamp() << "[tid=" << std::this_thread::get_id() << "]" << file << ":" << line << ":" << func << ") : ";
  }

  template<typename T>
  LogRecord &operator<<(const T &data) {
    ss_ << data;
    return *this;
  }

  LogRecord &operator<<(const std::string &data) {
    return *this << (data.c_str());
  }

  LogRecord &ref() {
    return *this;
  }

  std::string str() const {
    return ss_.str();
  }

 private:
  std::stringstream ss_;
};

class Logger {
 public:
  Logger &operator+=(LogRecord &record) {
#if defined(OS_OHOS)
      OH_LOG_DEBUG(LOG_APP, "%{public}s", record.str().c_str());
#else
      std::cout << record.str() << std::endl;
#endif
    return *this;
  }
};

Logger &GetLogger();

#define RTC_LOG GetLogger()+= LogRecord(__FILE_NAME__,  __LINE__, __func__).ref()

#endif //OHOS_DEMO_RTC_LOG_H
